﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.Neurons.Framework.Messages;
using JustinAngel.UnitedNationsNews.Pages.Main;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace JustinAngel.UnitedNationsNews.Framework
{
    public class BasePage : PhoneApplicationPage
    {
        public BasePage()
        {
            SetupPageTransitions();
            this.Loaded += new RoutedEventHandler(BasePage_Loaded);
        }


        protected virtual void SetupPageTransitions()
        {
            TransitionService.SetNavigationInTransition(this,
                new NavigationInTransition()
                {
                    Backward = new TurnstileTransition() { Mode = TurnstileTransitionMode.BackwardIn },
                    Forward = new TurnstileTransition() { Mode = TurnstileTransitionMode.ForwardIn },
                });
            TransitionService.SetNavigationOutTransition(this,
               new NavigationOutTransition()
               {
                   Backward = new TurnstileTransition() { Mode = TurnstileTransitionMode.BackwardOut },
                   Forward = new TurnstileTransition() { Mode = TurnstileTransitionMode.ForwardOut },
               });
        }


        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (ViewModel != null)
            {
                ViewModel.OnNavigatedFrom(e);
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (ViewModel != null)
            {
                ViewModel.OnNavigatedTo(e);
            }
        }

        void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            SetViewModel();
        }

        private void SetViewModel()
        {
            if (ViewModel != null && this.DataContext == null)
            {
                if (this.Tag != null)
                {
                    ViewModel.Data = this.Tag;
                }

                if (this.GetType() == typeof(MainPageViewModel))
                {
                    this.As<MainPageViewModel>().IsLoading = true;
                }

                SetDataContext();

                this.SetBinding(VSM.StateNameProperty, new Binding("CurrentStateName"));

                ViewModel.OnAfterAttachedToViewAsDataContext();
            }
        }

        protected virtual void SetDataContext()
        {
            this.DataContext = ViewModel;
        }

        #region ViewModel (DependencyProperty)

        /// <summary>
        /// Gets or sets the viewModel used in this page.
        /// </summary>
        public VViewModelBase ViewModel
        {
            get { return (VViewModelBase)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }
        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(VViewModelBase), typeof(BasePage),
              new PropertyMetadata(null));

        #endregion


        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            AddNavigatingText();

            DoBaseBackKey(e);

            //if (SystemTray.GetIsVisible(this))
            //{
            //    NavigatingText.Margin = new Thickness(24, 36, 0, 0);
            //}
        }

        protected void DoBaseBackKey(CancelEventArgs e)
        {
            ViewModel.OnBackkeyPress(e);

            base.OnBackKeyPress(e);
        }

        protected Grid AddNavigatingText()
        {
            var NavigatingText =  (Grid) XamlReader.Load(
                @"        <Grid  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" Height=""30"" VerticalAlignment=""Top"" Background=""#CCFFFFFF"">
        	<TextBlock HorizontalAlignment=""Left"" TextWrapping=""Wrap"" Text=""Navigating..."" Width=""129"" FontSize=""{StaticResource PhoneFontSizeNormal}"" Margin=""24,0,0,0"" FontFamily=""{StaticResource PhoneFontFamilySemiBold}""/>
        	<ProgressBar Style=""{StaticResource PerformanceProgressBar}"" RenderTransformOrigin=""0.5,0.5"" Margin=""135,0,0,0"" UseLayoutRounding=""False"" Background=""White"" IsIndeterminate=""True"" LargeChange=""0"" />
        </Grid>");
            this.Content.As<Grid>().Children.Add(NavigatingText);
            return NavigatingText;
        }
    }
}
